<?php


namespace app\common\model;
use think\Model;
use think\Exception;
class User extends Model
{
    protected $autoWriteTimestamp = true; //自动写入时间戳

    /**添加
     * @param array $data
     * @return bool
     * @throws \Exception
     */
    public function add($data=[])
    {
        if(!is_array($data)){
            exception('传递的数据不是数组');
        }
        $data['status'] = 1;
        //allowField(true)如果为true只允许写入表中字段
        $result = $this->data($data)->allowField(true)->save();
        return $result;
    }
    public function getUserByUsername($username){
        if(!$username){
            exception('用户名不合法');
        }
        $data ['username']=$username;
        return $this->where($data)->find();
    }

    //登录成功通过id写入登录时间
    public function upDataById($data,$id){
        return $this->allowField(true)->save($data,['id'=>$data]);
    }


    public function usernameById($id){
        $data = [
            'id'=>$id,
            'status'=>1,
        ];

        $rel = $this->where($data)->select();

        return $rel;
    }


}
